﻿//---------------------------------------CA310
//using CA200SRVRLib;
//---------------------------------------CA410
using CASDK2;


namespace Module_Debug.Hardware
{
    public class CaSDK
    {
        //---------------------------------------CA310
        //public Ca objCa;
        //public Probe objProbe;
        //public Memory objMemory;

        //Ca200 objCa200 = new Ca200();
        //---------------------------------------CA410
        static CASDK2Ca200 objCa200_410;
        //static CASDK2Cas objCas_410;
        static CASDK2Ca objCa_410;
        //static CASDK2Probes objProbes;
        //static CASDK2OutputProbes objOutputProbes;
        static CASDK2Probe objProbe_410;
        static CASDK2Memory objMemory_410;
        //---------------------------------------
        static int err = 0;
        static int CA_SelID = 1;// record have initaled type 0=CA310;1=CA410;2-CMA01

        const int MAXPROBE = 10;
        //display mode
        const int MODE_Lvxy = 0;
        const int MODE_Tduv = 1;
        const int MODE_Lvduv = 5;
        const int MODE_FMA = 6;
        const int MODE_XYZ = 7;
        const int MODE_JEITA = 8;
        const int MODE_lvPeld = 9;
        const int MODE_Waveform = 10;
        const int MODE_Contrast_flocker = 11;
        //AveragingMode
        const int SLOW = 0;
        const int FAST = 1;
        const int LTD_AUTO = 2;
        const int AUTO = 3;
        //synchronization mode
        const int NTSC = 0;
        const int PAL = 1;
        const int EXT = 2;
        const int UNIV = 3;
        const int INT = 4;
        const int MANUAL = 5;

        public double pVal = 0.0;
        public double Lv = 0.0;
        public double sx = 0.0;
        public double sy = 0.0;
        public double X = 0.0;
        public double Y = 0.0;
        public double Z = 0.0;
        public double T = 0.0;
        public double duv = 0.0;
        public double JEITA = 0.0;
        public double FMA = 0.0;

        const int RED = 0;
        const int GREEN = 1;
        const int BLUE = 2;
        const int WHITE = 3;

        public double R_x = 0.67;//
        public double R_y = 0.33;
        public double G_x = 0.22;
        public double G_y = 0.71;
        public double B_x = 0.14;
        public double B_y = 0.06;

        public double E4 = 0.315;
        public double E5 = 0.71;
        public double E6 = 0.047;
        public double D4 = 0.618;
        public double D5 = 0.25;
        public double D6 = 0.143;

        public double auto_frq;
        public double auto_lum;//
        public double auto_x;
        public double auto_y;
        public double auto_T;
        public double auto_duv;
        public bool auto_connetflag = false;
        public CaSDK()
        {

        }
        //============================CA310 Start============================================
        public void CA310_Connect()
        {
            try
            {
                //objCa200.AutoConnect();
                return;
            }
            catch {; }
        }
        public void CA310_Initial(byte channel)
        {
            CA_SelID = 0;
            //objCa = objCa200.SingleCa;
            //objProbe = objCa.SingleProbe;
            //objMemory = objCa.Memory;
            //objCa.CalZero();
            //objCa.SyncMode = 0;//0--NTSC  1--PAL  2--EXT  3--UNIV  4--INT
            //objCa.AveragingMode = 1;//0--slow  1--fast 2--auto 
            //objCa.DisplayMode = 0;
            //objMemory.ChannelNO = channel;
        }
        //============================CA310 End============================================

        //============================CA410 Start============================================
        public void DefaultSetting(byte channel)
        {
            int freqmode = 4;   // SyncMode : INT 
            double freq = 60.0; //frequency = 60.0Hz
            int speed = 1;      //Measurement speed : FAST
            int Lvmode = 1;     //Lv : cd/m2

            err = objCa_410.CalZero();                       //Zero-Calibration
            err = objCa_410.put_DisplayProbe("P1");          //Set display probe to P1
            err = objMemory_410.put_ChannelNO(channel);
            err = objCa_410.put_SyncMode(freqmode, freq);    //Set sync mode and frequency
            err = objCa_410.put_AveragingMode(speed);        //Set measurement speed
            err = objCa_410.put_BrightnessUnit(Lvmode);      //SetBrightness unit
        }
        public int ca310speed = 1;
        private byte casdk_mode = 0;
        public void SingleCalibration(byte MODE, byte channel, double freq_set)
        {
            CA_SelID = 1;
            casdk_mode = MODE;
            switch (casdk_mode)
            {
                case 0: //MODE_Lvxy
                    {
                        err = objCa_410.put_DisplayProbe("P1");
                        err = objMemory_410.put_ChannelNO(channel);
                        err = objCa_410.put_DisplayMode(MODE);//Mode:Color Lvxy
                        err = objCa_410.CalZero(); //3.4.2 - zero-calibration
                        err = objCa_410.SetLvxyCalMode();
                        err = objCa_410.put_SyncMode(INT, freq_set); //sync mode and frequency
                        err = objCa_410.put_AveragingMode(ca310speed);  //measure speed  /FAST SLOW
                        err = objCa_410.Measure();
                        err = objCa_410.Enter();
                    }
                    break;
                case 8: //MODE_JEITA
                    {
                        err = objMemory_410.put_ChannelNO(channel);
                        err = objCa_410.put_DisplayMode(MODE);//Mode:Color Lvxy
                        err = objCa_410.CalZero(); //3.4.2 - zero-calibration
                    }
                    break;
                default:
                    break;
            }

        }
        public void CA410_Measure_Lxy()
        {
            try
            {
                err = objCa_410.Measure();
                switch (casdk_mode)
                {
                    case 0: //MODE_Lvxy
                        {
                            objProbe_410.get_Lv(ref Lv);
                            objProbe_410.get_sx(ref sx);
                            objProbe_410.get_sy(ref sy);

                        }
                        break;
                    case 8: //MODE_JEITA
                        {
                            //CA410_Measure_JEITA();
                            objProbe_410.get_FlckrJEITA(ref JEITA);
                        }
                        break;
                    default:
                        break;
                }
            }
            catch {; }
        }
        public void CA410_Measure_JEITA()
        {
            err = objCa_410.put_DisplayMode(MODE_JEITA);//Mode:Flicker JEITA
            err = objCa_410.Measure();
            err = objProbe_410.get_FlckrJEITA(ref JEITA);
        }
        public void CA410_Measure_FMA(byte channel)
        {
            err = objCa_410.put_DisplayMode(MODE_FMA);//Mode:Flicker JEITA
            err = objCa_410.Measure();
            err = objProbe_410.get_FlckrFMA(ref FMA);
        }
        public void CA410_Measure_XYZ()
        {
            int lSampleCountIndex = 8192;
            int lSamplingInterval = 1;
            int lXYZSensor = 1;
            err = objCa_410.put_DisplayMode(MODE_XYZ);//Mode:Flicker JEITA
            err = objCa_410.SetWaveformParams(lSampleCountIndex, lSamplingInterval, lXYZSensor);
            err = objCa_410.Measure();
            objProbe_410.get_PeakFrequency(ref pVal);
        }
        public void AutoConnect()
        {
            objCa200_410 = new CASDK2Ca200(); // Generate application object
            try
            {
                err = objCa200_410.AutoConnect();
                err = objCa200_410.get_SingleCa(ref objCa_410);
                err = objCa_410.get_Memory(ref objMemory_410);
                err = objCa_410.get_SingleProbe(ref objProbe_410);
                auto_connetflag = true;
            }
            catch {; }
        }
        public void Disconnect()
        {
            //System.Threading.Thread.Sleep(10);
            //Disconnect CA-410
            if (auto_connetflag == true)
            {
                err = objCa200_410.AutoDisconnect();
            }
            else
            {
                err = objCa200_410.DisconnectAll();
            }
        }
        //============================CA410 End============================================

        public void YUV_to_RGB(double D_y, double D_x, out double Lr_d, out double Lb_d, out double Lg_d)
        {
            Lg_d = (E5 / D_y) * ((E4 - D_y) * (D6 * E4 - D4 * E6) - (D_x * E4 - D4 * D_y) * (E4 - E6)) / ((E4 - E5) * (D6 * E4 - D4 * E6) - (D5 * E4 - D4 * E5) * (E4 - E6));
            Lb_d = ((1 / D_y - 1 / E4) - Lg_d * (1 / E5 - 1 / E4)) / (1 / E6 - 1 / E4);
            Lr_d = 1 - Lg_d - Lb_d;

            // Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            // Lb_d = 1.0;
            // Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));
        }

        public void Lvxy_to_XYZ(double l_Lv, double l_x, double l_y, out double L_X, out double L_Y, out double L_Z)
        {
            L_X = l_Lv * l_x / l_y;
            L_Y = l_Lv;
            L_Z = (1 - l_x - l_y) * l_Lv / l_y;
        }

        public void CASDK_AutoMeasure()
        {
            try
            {
                if (CA_SelID == 0)
                {
                    //objCa.Measure();
                    //Lv = objProbe.Lv;
                    //sx = objProbe.sx;
                    //sy = objProbe.sy;
                    //auto_T = objProbe.T;
                    //auto_duv = objProbe.duv;
                    //T = objProbe.T;
                    //duv = objProbe.duv;
                    //X = objProbe.X;
                    //Y = objProbe.Y;
                    //Z = objProbe.Z;
                }
                else if (CA_SelID == 1)
                {

                    err = objCa_410.Measure();
                    objProbe_410.get_Lv(ref Lv);
                    objProbe_410.get_sx(ref sx);
                    objProbe_410.get_sy(ref sy);

                    objProbe_410.get_T(ref T);
                    objProbe_410.get_duv(ref duv);
                    objProbe_410.get_X(ref X);
                    objProbe_410.get_Y(ref Y);
                    objProbe_410.get_Z(ref Z);

                }
                //else if (CA_Sel.SelectedIndex == 2)
                //{
                //    CMadj.CMA01_Measure_Lxy();
                //    CASDK_LUM = CMadj.Lv;
                //    CASDK_x = CMadj.sx;
                //    CASDK_y = CMadj.sy;
                //}
                //Msr_Drawing_Curve(CASDK_LUM, CASDK_x, CASDK_y);
            }
            catch
            {

            }
        }
    }
}
